local p = {}
-- 通用
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')
local core_map = require('Module:Util/Core')
local ELEMENT_MAP = require('Module:Util/Config').ELEMENT_MAP

---------------------------------------------------------------------------
-- 常量
---------------------------------------------------------------------------

local TYPE_LIST = {'火', '水', '地', '风', '光', '暗', '可变化'}

local TYPE_SET_MAP = {
	['火'] = {s='#FF6565', t='火属性'},
	['水'] = {s='#69A9FF', t='水属性'},
	['地'] = {s='#C88018', t='地属性'},
	['风'] = {s='#77EE88', t='风属性'},
	['光'] = {s='#EACC01', t='光属性'},
	['暗'] = {s='#B091FF', t='暗属性'},
	['可变化'] = {s='', t='可变化属性'},
}

---------------------------------------------------------------------------
-- 获取数据
---------------------------------------------------------------------------

function p.query_data(rarity)
	-- 查询数据

	local result = core_map

	-- 按属性整理数据
	local output = {}
	for _, element in ipairs(TYPE_LIST) do
		output[element] = {}
	end

	for _, card_info in pairs(core_map) do
	    card_info.ID = _
		if (tonumber(card_info.ID) or 0) > 0 and (card_info.addDate or '') ~= '' then
		    if (rarity or '') == '' or (card_info.rarity == rarity) then
				table.insert(output[ELEMENT_MAP[card_info.element]], card_info)
			end
		end
	end
	
	for _, element in ipairs(TYPE_LIST) do
		table.sort(output[element], p.sort_core_data)
	end

	return output
end

function p.sort_core_data(a, b)
    if (a.addDate ~= b.addDate) then
		return a.addDate < b.addDate
	elseif (a.rarity ~= b.rarity) then
		return a.rarity > b.rarity
	elseif (a.series ~= b.series) then
		return a.series < b.series
	else
		return a.ID < b.ID
	end
end

function p.reverseTable(tab)
	local tmp = {}
	for i = 1, #tab do
		local key = #tab
		tmp[i] = table.remove(tab)
	end

	return tmp
end

-----------------------------------------------------------------------------
-- 回响信息页生成
-----------------------------------------------------------------------------

function p.core_list(frame)
	local args = getArgs(frame)

	local rarity = args[1] or ''
	local picture_l = picture
	local bool = true
	local core_data = p.query_data(rarity)

	local html = mw.html.create()

	for _, element in ipairs(TYPE_LIST) do
		if #core_data[element] > 0 then
			local type_info = TYPE_SET_MAP[element]

			local h2 = html:tag('h2'):cssText('font-size:24px;'):addClass('shadow-text'):wikitext(type_info.t)
			if type_info.s ~= '' then
				h2:cssText('color:'..type_info.s)
			end

			local flex_list = html:tag('div'):addClass('flex-list m-flex-list'):cssText('text-align: center;font-size:14px;')

			for _, card_info in ipairs(core_data[element]) do
				local link = 'Core/'..card_info.ID
				flex_list:tag('div'):addClass('flex-item core-box'):tag('div'):addClass('image'):wikitext('[[File:'..card_info.ID..'_01.png|link='..link..']]'):done()
				:tag('div'):addClass('text'):wikitext('[['..link..'|'..(card_info.tag_title or card_info.name)..']]'):done()
			end
		end
	end

	return html

	-- body
end


return p
